﻿using System;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using ServiceHelper;

namespace QLNXe
{
    public partial class Form1 : Form
    {
        VeXe frmVeXe = new VeXe();
        public static string user;
        public static string pass;

        public Form1()
        {
            InitializeComponent();
        }

        private void openImgToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog Openfile = new OpenFileDialog();
            if (Openfile.ShowDialog() == DialogResult.OK)
            {
                Image<Bgr, Byte> img;
                try
                {
                    img = new Image<Bgr, byte>(Openfile.FileName);
                }
                catch
                {
                    MessageBox.Show(String.Format("Invalide File: {0}", Openfile.FileName));
                    return;
                }
                imageBox1.Image = img;
                ImageProcessing imageProcessing = new ImageProcessing();
                string bienSo = filterCharactor(imageProcessing.ProcessImage(img));
                txtBienSo.Text = bienSo;
                TicketType? objPlate = WebServiceHelper.CheckPlate(Constant.WebserviceUrl.CheckTicketType, user,
                                                                    pass, bienSo);
                MotorbikeType? objMotorType = WebServiceHelper.CheckMotorType(Constant.WebserviceUrl.CheckMotorType,
                                                                              user, pass, "29H104178");
                if (objPlate == null)
                {
                    //txtLoaiXe.Text = motorType;
                    txtBienSo.Enabled = true;
                    txtLoaiVe.Text = "Vé Ngày";
                    txtGiaVe.Text = "3000";
                }
                else
                {
                    //int price = Int32.Parse(BusinessUtils.GetValue(obj, "Price"));
                    //string motorType = BusinessUtils.GetValue(obj, "Description");
                    //TicketType ticketType = TicketType

                    //Fill to textbox
                    if (objMotorType.ToString() == "XE_GA")
                    {
                        rbtnXeGa.Checked = true;
                        rbtnXeSo.Enabled = false;
                    }
                    else
                        if (objMotorType.ToString() == "XE_SO")
                        {
                            rbtnXeSo.Checked = true;
                            rbtnXeGa.Enabled = false;
                        }
                    txtLoaiVe.Text = objPlate.ToString();
                    txtGiaVe.Text = "0";
                }
            }
        }

        // delegate
        public delegate void PassData(TextBox bienSoXe, TextBox loaiVe, TextBox giaVe);

        private void btnInVe_Click(object sender, EventArgs e)
        {
            PassData pass = new PassData(frmVeXe.GetData);
            pass(this.txtBienSo, this.txtLoaiVe, this.txtGiaVe);
            frmVeXe.ShowDialog();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        protected string filterCharactor(string text)
        {
            StringBuilder sb = new StringBuilder();
            var lastWasInvalid = false;
            foreach (char c in text)
            {
                if (char.IsLetterOrDigit(c))
                {
                    sb.Append(c);
                    lastWasInvalid = false;
                }
                else
                {
                    if (!lastWasInvalid)
                        sb.Append("");
                    lastWasInvalid = true;
                }
            }

            return sb.ToString();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.ExitThread();
        }
    }
}